rm(list = ls(all = TRUE))
pacman::p_load(ggplot2, dplyr, magrittr, lazyopt, stringr)

arg <- c("-i", "E:/projects/R_draw_sc/yun/goko/data/cc.Go.bar.dat", "-o", "E:/projects/R_draw_sc/yun/goko/image",
         "-n", "20", "-sm", "-1")

opt <- matrix(c("barwidth", "br", 1, "numeric", "Width of column", "0.9",
                "ymax", "yr", 1, "character", "The Y-axis", "ymax",
                "nums", "n", 1, "numeric", "Sets the number of rows to be read randomly", "-1",
                "stringMax", "sm", 1, "numeric", "Sets the maximum length of the Y-axis string", "-1",
                "colorstyle", "cs", 1, "character", "Set the color to ':' split", "#E41A1C:#FFC0CB:#1E90FF:#00BFFF:#FF8C00:#FFDEAD:#4DAF4A:#90EE90:#9692C3:#CDB4FF:#40E0D0:#00FFFF",
                "tablepath", "i", 2, "character", "The path to the table data read", "",
                "filepath", "o", 2, "character", "The package path of the output image", "",
                "star_size", "ss", 1, "numeric", "set star size", "7",
                "imageSize", "is", 1, "character", "The height and width of the picture", "20:14",
                "xtext_style", "xts", 1, "character", "X text style Font:font type:font size", "sans:bold.italic:13",
                "ytext_style", "yts", 1, "character", "Y text style Font:font type:font size", "sans:bold.italic:14",
                "xlab_style", "xls", 1, "character", "X lab style Font:font type:font size:name", "sans:bold.italic:12: ",
                "ylab_style", "yls", 1, "character", "Y lab style Font:font type:font size:name", "sans:bold.italic:12: ",
                "main_style", "ms", 1, "character", "Main style Font:font type:font size:name", "sans:bold.italic:12:Gene number",
                "legendtext_style", "lts", 1, "character", "Legend text style Font:font type:font size", "sans:bold.italic:15",
                "legendtitle_style", "lms", 1, "character", "Legend title style Font:font type:font size:name", "sans:bold.italic:15:up    down",
                "resolution", "dpi", 1, "numeric", "Set the resolution to allow 72,96,300 or 600", "300",
                "imageformt", "if", 1, "character", "pdf,tiff,png", "png",
                "imageName", "in", 1, "character", "picture name", "Go",
                "help", "h", 0, "logical", "Help document", "TRUE"
), byrow = TRUE, ncol = 6) %>% lazyopt(arg)

main_style <- opt$main_style %>% fenge()

xtext_style <- opt$xtext_style %>% fenge()

ytext_style <- opt$ytext_style %>% fenge()

xlab_style <- opt$xlab_style %>% fenge()

ylab_style <- opt$ylab_style %>% fenge()

legendtext_style <- opt$legendtext_style %>% fenge()

legendtitle_style <- opt$legendtitle_style %>% fenge()

imageSize <- opt$imageSize %>% fenge()

strmax <- opt$stringMax

colorstyle <- opt$colorstyle %>% fenge()

resolution <- opt$resolution; if (resolution != 72 &&
  resolution != 96 &&
  resolution != 300 &&
  resolution != 600) { resolution <- 300 }
ss <- "sans:bold.italic:15:pvalue  <= 0.001  ***\n\n0.001 < pvalue  <= 0.01  ** \n\n0.01 < pvalue  <= 0.05  *\n
 \n\n"
dd <- opt$legendtitle_style %>% fenge()
dd <- paste0(ss, dd[4])
opt$legendtitle_style <- paste0(dd)
legendtitle_style <- opt$legendtitle_style %>% fenge()


data <- opt$tablepath %>% read.delim(header = TRUE, check.names = FALSE, row.names = 1)
if (opt$nums > 0) { data %<>% head(n = opt$nums) }

if (ncol(data) == 6) {
  colnames(data) <- c("color", "x", "all", "up", "down", "p")
  usedata <- data.frame(
    color = c(paste0(data$color %>% as.character(), rep("up", nrow(data))), paste0(data$color %>% as.character(), rep("down", nrow(data)))),
    x = data$x %>% as.character() %>% rep(2),
    y = c(data$up %>% as.numeric(), data$down %>% as.numeric(0)),
    p = c(rep(1, nrow(data)), data$p %>% as.numeric())
  ) %>% arrange(color)
}

if (ncol(data) == 4) {

}
usedata$x %<>% str_sub(end = opt$stringMax)
usedata$x %<>% factor(levels = usedata$x %>% as.character() %>% unique())

col <- lazyopt::fenge(opt$colorstyle)[1:(usedata$color %>% unique() %>% length())] %>% rev()

labels <- usedata$color %>% unique() %>% as.character()
usedata %<>% mutate(star = case_when(p <= 0.001 ~ "      ***",
                                     p > 0.001 & p <= 0.01 ~ "      **",
                                     p > 0.01 & p <= 0.05 ~ "      *")) %>% select(1, 2, 3, 5)

labels[seq(2, length(labels), 2)] <- ""
labels[seq(1, length(labels), 2)] <- data %>% arrange(color) %$% unique(color) %>% as.character()

ggplot(usedata) +
  geom_col(aes(x = x, y = y, fill = color), position = "stack") +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.background = element_blank()
  ) +
  scale_y_continuous(expand = c(0, 0), guide = guide_axis(position = "top")) +
  coord_flip() +
  scale_fill_manual(values = col, labels = labels, name = legendtitle_style[4]) +
  guides(fill = guide_legend(reverse = T, ncol = 2, byrow = TRUE)) +
  labs(title = main_style[4], y = ylab_style[4], x = xlab_style[4]) +
  theme(
    axis.text.y = element_text(family = ytext_style[1], face = ytext_style[2], size = as.numeric(ytext_style[3])),
    axis.text.x = element_text(family = xtext_style[1], face = xtext_style[2], size = as.numeric(xtext_style[3])),
    legend.justification = c("right", "top"),
    legend.text = element_text(family = legendtext_style[1], size = as.numeric(legendtext_style[3]), face = legendtext_style[2]),
    legend.title = element_text(size = as.numeric(legendtitle_style[3]), family = legendtitle_style[1], face = legendtitle_style[2]),
    title = element_text(size = as.numeric(main_style[3]), family = main_style[1], face = main_style[2]),
    axis.title.x = element_text(size = as.numeric(xlab_style[3]), family = xlab_style[1], face = xlab_style[2]),
    axis.title.y = element_text(size = as.numeric(ylab_style[3]), family = ylab_style[1], face = ylab_style[2])
  ) +
  theme(axis.line.x = element_line(arrow = arrow(length = unit(0, 'cm'), type = "open")),
        plot.title = element_text(hjust = 0.5)
  ) +
  geom_text(aes(x = x, y = y, label = star), nudge_x = 0)
